//
//  BaseViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/7.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
//默认返回按钮
@property (nonatomic, strong) UIButton *backBtn;

@property (nonatomic, assign) BOOL isStatusBarNormal;

@end

@implementation BaseViewController

#pragma mark Init && Add
- (UIImageView *)line
{
    if (!_line) {
        _line = [[UIImageView alloc] initWithFrame:CGRectMake(0, NAVIBAR_HEIGHT-0.5, SCREEN_WIDTH, 0.5)];
        _line.backgroundColor = [UIColor grayColor];
    }
    return _line;
}

- (UIButton *)backBtn
{
    if (!_backBtn)
    {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        _backBtn.frame = CGRectMake(5, NAVIBAR_HEIGHT - 44, 44, 44);
        _backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    }
    return _backBtn;
}

- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(5, NAVIBAR_HEIGHT - 44, 44, 44);
        _leftButton.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(SCREEN_WIDTH - 5 - 44, NAVIBAR_HEIGHT - 44, 44, 44);
        _rightButton.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    }
    return _rightButton;
}

- (UIImageView *)topTitleView
{
    if (!_topTitleView) {
        _topTitleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NAVIBAR_HEIGHT)];
        _topTitleView.backgroundColor = JSAMainColor;
        _topTitleView.userInteractionEnabled = YES;
        [self.view addSubview:_topTitleView];
    }
    return _topTitleView;
}

- (UIImageView *)navBgImageView
{
    if (!_navBgImageView) {
        _navBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NAVIBAR_HEIGHT)];
        _navBgImageView.backgroundColor = [UIColor clearColor];
        _navBgImageView.userInteractionEnabled = YES;
    }
    return _navBgImageView;
}

- (UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconImgView;
}

- (UILabel *)topTitleLabel
{
    if (!_topTitleLabel)
    {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
        _topTitleLabel.textColor = [UIColor whiteColor];
        _topTitleLabel.backgroundColor = [UIColor clearColor];
        _topTitleLabel.font = kMainTitleFont;
        _topTitleLabel.frame = CGRectMake((SCREEN_WIDTH - 200)/2, NAVIBAR_HEIGHT - 44, 200, 44);
    }
    return _topTitleLabel;
}

- (UIButton *)secondLeftButton
{
    if (!_secondLeftButton) {
        _secondLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _secondLeftButton.frame = CGRectMake(5+44, NAVIBAR_HEIGHT - 44, 44, 44);
        _secondLeftButton.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    }
    return _secondLeftButton;
}

- (UIButton *)secondRightButton
{
    if (!_secondRightButton) {
        _secondRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _secondRightButton.frame = CGRectMake(SCREEN_WIDTH - 5 - 44 - 44, NAVIBAR_HEIGHT - 44, 44, 44);
        _secondRightButton.imageEdgeInsets = UIEdgeInsetsMake(0,15,0,0);
    }
    return _secondRightButton;
}

- (UILabel *)leftLabel
{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_leftButton.frame)+5, NAVIBAR_HEIGHT - 44, CGRectGetMinX(_topTitleLabel.frame)- CGRectGetMaxX(_leftButton.frame)-5, 44)];
        _leftLabel.textColor = [UIColor whiteColor];
        _leftLabel.backgroundColor = [UIColor clearColor];
        _leftLabel.font = kDescTitleFont;
    }
    return _leftLabel;
}


#pragma mark System Action
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = JSAMainBgColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeStatusBarFrame) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    [self changeStatusBarFrame];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (!self.disablePopGesture)
    {
        BaseViewController *weakSelf = self;
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
        {
            self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
    return !self.disablePopGesture;
}

- (void)changeStatusBarFrame
{
    int height = SCREEN_HEIGHT;
    [self.view setHeight:height];
    
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]] && view.height == (SCREEN_HEIGHT - 20))
        {
            [view setHeight:height];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)dealloc
{
    NSString *str = [NSString stringWithFormat:@"%@ dealloc",[NSString stringWithUTF8String:object_getClassName(self)]];
    NSLog(@"%@",str);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark Other Action
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


//默认绿色返回按钮
- (void)setDefaultNav
{
    [self.topTitleView addSubview:self.backBtn];
}

//默认绿色返回按钮/标题
- (void)setDefaultNavWithTitle:(NSString *)title
{
    [self setDefaultNav];
    [self setTitle:title];
}

//默认绿色返回按钮／标题／右侧按钮
- (void)setDefaultNavWithTitle:(NSString *)title WithRightButton:(UIImage *)rightImage
{
    [self setDefaultNavWithTitle:title];
    [self setRightButtonImage:rightImage];
}

//默认绿色返回按钮／右侧按钮
- (void)setDefaultNavWithRightButton:(UIImage *)rightImage
{
    [self setDefaultNavWithTitle:nil WithRightButton:rightImage];
}

//左侧按钮／右侧按钮
- (void)setLeftButton:(UIImage *)leftImage WithRightButton:(UIImage *)rightImage
{
    [self setLeftButton:leftImage WithTitle:nil WithRightButton:rightImage];
}

//左侧按钮／标题／右侧按钮
- (void)setLeftButton:(UIImage *)leftImage WithTitle:(NSString *)title WithRightButton:(UIImage *)rightImage
{
    [self setTitle:title];
    [self setLeftButtonImage:leftImage];
    [self setRightButtonImage:rightImage];
}


//左侧标题／标题／右侧标题
- (void)setLeftTitle:(NSString *)leftTitle WithTitle:(NSString *)title WithRightTitle:(NSString *)rightTitle
{
    [self setTitle:title];
    [self setLeftTitle:leftTitle];
    [self setRightTitle:rightTitle];
}

//左侧标题
- (void)setLeftTitle:(NSString *)leftTitle
{
    [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
    [self.leftButton setTitle:leftTitle forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.leftButton];
}
//右侧标题
- (void)setRightTitle:(NSString *)rightTitle
{
    [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
    [self.rightButton setTitle:rightTitle forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.rightButton];
}

//左侧按钮
- (void)setLeftButtonImage:(UIImage *)image
{
    [self.leftButton setImage:image forState:UIControlStateNormal];
    [self.leftButton setImage:image forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.leftButton];
}

//右侧按钮
- (void)setRightButtonImage:(UIImage *)image
{
    [self.rightButton setImage:image forState:UIControlStateNormal];
    [self.rightButton setImage:image forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.rightButton];
}

//标题
- (void)setTitle:(NSString *)title
{
    [self.topTitleView addSubview:self.topTitleLabel];
    self.topTitleLabel.text = title;
}

//右按钮标题
- (void)setRightTitle:(NSString *)title TitleColor:(UIColor *)titleColor
{
    [self.topTitleView addSubview:self.rightButton];
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton setTitleColor:titleColor forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
}

//nav背景颜色
- (void)setNavBackgroundColor:(UIColor *)color
{
    self.topTitleView.backgroundColor = color;
}

//nav透明
- (void)setNavBackgroundClear
{
    self.topTitleView.backgroundColor = [UIColor clearColor];
}

//nav背景图片
- (void)setNavBackgroundImage:(UIImage *)bgImage
{
    [self setNavBackgroundClear];
    self.navBgImageView.image = bgImage;
    [self.topTitleView addSubview:self.navBgImageView];
    [self.topTitleView sendSubviewToBack:self.navBgImageView];
}

//左侧第二按钮
- (void)setSecondLeftButtonImage:(UIImage *)image
{
    [self.secondLeftButton setImage:image forState:UIControlStateNormal];
    [self.secondLeftButton setImage:image forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.secondLeftButton];
}

//右侧第二按钮
- (void)setSecondRightButtonImage:(UIImage *)image
{
    [self.secondRightButton setImage:image forState:UIControlStateNormal];
    [self.secondRightButton setImage:image forState:UIControlStateHighlighted];
    [self.topTitleView addSubview:self.secondRightButton];
}

//左侧第一Label
- (void)setLeftLabelText:(NSString *)info
{
    self.leftLabel.text = info;
    [self.topTitleView addSubview:self.leftLabel];
}

//底部横线
- (void)setBottomLine
{
    [self.topTitleView addSubview:self.line];
}

- (void)seticonImgView:(UIImage *)icon
{
    [self.topTitleView addSubview:self.iconImgView];
    self.iconImgView.image = icon;
}

@end
